package com.suray.basic.wcs.plc.pojo.oper.lifter.message;

import com.suray.basic.wcs.utils.comm.BaseMessage;
import com.suray.basic.wcs.utils.typedef.Byte;
import com.suray.basic.wcs.utils.typedef.*;

public class LifterWriteMessage extends BaseMessage {
    public LifterWriteMessage(String series, int startAddress, BaseDataType[] data) {
        super(series + "写入", true);

        int size = 0;
        for (BaseDataType dataType : data) {
            if (dataType instanceof Bit) {
                size += 1;
            } else if (dataType instanceof Byte) {
                size += 8;
            } else if (dataType instanceof Word) {
                size += 16;
            } else if (dataType instanceof DWord) {
                size += 32;
            }
        }

        generalData(startAddress, size / 16);

        for (BaseDataType dataType : data) {
            baseDatas.add(dataType);
        }
    }

    public LifterWriteMessage(String series, int startAddress, BaseDataType data) {
        super(series + "写入", true);

        int size = 0;
        if (data instanceof Bit) {
            size += 1;
        } else if (data instanceof Byte) {
            size += 8;
        } else if (data instanceof Word) {
            size += 16;
        } else if (data instanceof DWord) {
            size += 32;
        }

        generalData(startAddress, size / 16);

        baseDatas.add(data);
    }

    private void generalData(int startAddress, int dataLength) {
        baseDatas.add(new Byte("固定1", "固定值"));
        baseDatas.add(new Byte("固定2", "固定值"));
        baseDatas.add(new Byte("固定3", "固定值"));
        baseDatas.add(new Byte("固定4", "固定值"));
        baseDatas.add(new Byte("固定5", "固定值"));
        baseDatas.add(new Byte("length", "数据长度"));
        baseDatas.add(new Byte("deviceAddress", "设备地址"));
        baseDatas.add(new Byte("cmd", "命令类型"));
        baseDatas.add(new Word("startAddress", "起始地址"));
        baseDatas.add(new Word("addressNum", "地址数"));
        baseDatas.add(new Byte("writeLength", "写入数据长度"));

        for (int i = 1; i < 6; i++) {
            setValue("固定" + i, 0);
        }

        setValue("length", dataLength * 2 + 7);
        setValue("deviceAddress", 1);
        setValue("cmd", 16);
        setValue("startAddress", startAddress);
        setValue("addressNum", dataLength);
        setValue("writeLength", dataLength * 2);
    }
}
